package cloud.alibaba.study.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cloud.alibaba.study.entity.base.ResponseResult;
import cloud.alibaba.study.entity.order.OrderEntity;
import cn.hutool.core.util.IdUtil;

@RestController
@RequestMapping("order")
public class OrderController {
	
	@Value("${server.port}")
	private String serverPort;
	@Value("${spring.application.name}")
	private String springApplicationName;

	
	private static Map<String,OrderEntity> orderInfo = new HashMap<String,OrderEntity>();
	private static List<OrderEntity> orderList = new ArrayList<OrderEntity>();
	
	
	static {
		OrderEntity o1 = new OrderEntity();
		o1.setOrderId(IdUtil.getSnowflake().nextIdStr());
		o1.setPrice(new BigDecimal("41.99"));
		o1.setAmount(new BigDecimal("2"));
		o1.setOrderPrice(o1.getPrice().multiply(o1.getAmount()));
		o1.setOrderState(0);
		orderInfo.put("1", o1);
		
		OrderEntity o2 = new OrderEntity();
		o2.setOrderId(IdUtil.getSnowflake().nextIdStr());
		o2.setPrice(new BigDecimal("4.58"));
		o2.setAmount(new BigDecimal("2"));
		o2.setOrderPrice(o2.getPrice().multiply(o2.getAmount()));
		o2.setOrderState(0);
		orderInfo.put("2", o2);
		
		OrderEntity o3 = new OrderEntity();
		o3.setOrderId(IdUtil.getSnowflake().nextIdStr());
		o3.setPrice(new BigDecimal("8.99"));
		o3.setAmount(new BigDecimal("2"));
		o3.setOrderPrice(o3.getPrice().multiply(o3.getAmount()));
		o3.setOrderState(0);
		orderInfo.put("3", o3);
		
		orderList.add(o1);
		orderList.add(o2);
		orderList.add(o3);
	}
	
	@GetMapping("/getOrderList")
	public ResponseResult<Object> getOrder(){
		ResponseResult<Object> res = new ResponseResult<Object>();
		return res.success(orderList,"服务端口：" +  serverPort);
	}
	
	@GetMapping("/getOrderInfo/{id}")
	public ResponseResult<OrderEntity> getOrderInfo(@PathVariable String id){
		ResponseResult<OrderEntity> res = new ResponseResult<OrderEntity>();
		if(StringUtils.isBlank(id)) {
			return res.fail("id不能为空！");
		}
		return res.success(orderInfo.get(id),"服务端口：" + serverPort);
	}
	

}
